<template>
	<view class="enhanced-goods-item" @tap="handleClick">
		<view class="goods-card">
			<!-- 商品图片容器 -->
			<view class="image-container">
				<cl-image
					:src="item.mainPic || item.goodsInfo?.mainPic"
					:alt="item.title"
					class="goods-image"
					mode="aspectFill"
					:lazy-load="true"
					fallback="/static/empty/no.jpg"
					@error="handleImageError"
				/>

				<!-- 图片遮罩层 -->
				<view class="image-overlay"></view>

				<!-- 收藏按钮 -->
				<view class="collect-btn" @tap.stop="handleCollect">
					<cl-icon
						:name="item.isCollected ? 'heart-fill' : 'heart'"
						size="20"
						:color="item.isCollected ? '#ff4757' : '#ffffff'"
					></cl-icon>
				</view>

				<!-- 标签区域 -->
				<view class="tags-area" v-if="showTags">
					<view class="tag prescription-tag" v-if="item.isPres === 1">处方</view>
					<view class="tag stock-tag" v-if="isLowStock">库存紧张</view>
					<view class="tag near-expiry-tag" v-if="hasNearExpiry">近效期</view>
				</view>
			</view>

			<!-- 商品信息 -->
			<view class="goods-info">
				<!-- 商品标题 -->
				<view class="goods-title">
					<text class="title-text">{{ item.title || item.goodsInfo?.title }}</text>
				</view>

				<!-- 商品规格 -->
				<view class="goods-spec" v-if="item.goodsSpec || item.goodsInfo?.goodsSpec">
					<text class="spec-text">{{ item.goodsSpec || item.goodsInfo?.goodsSpec }}</text>
				</view>

				<!-- 价格区域 -->
				<view class="price-area">
					<view class="price-main">
						<text class="price-symbol">¥</text>
						<text class="price-value">{{
							formatPrice(item.price || item.goodsInfo?.price || 0)
						}}</text>
					</view>
					<view class="price-info" v-if="showPriceInfo">
						<text class="original-price" v-if="originalPrice"
							>¥{{ formatPrice(originalPrice) }}</text
						>
						<text class="sales-info">已售{{ item.sold || 0 }}</text>
					</view>
				</view>

				<!-- 商品属性 -->
				<view class="goods-attrs" v-if="showAttrs">
					<view
						class="attr-item"
						v-if="item.manufacturer || item.goodsInfo?.manufacturer"
					>
						<text class="attr-text">{{
							item.manufacturer || item.goodsInfo?.manufacturer
						}}</text>
					</view>
					<view class="attr-item" v-if="stockInfo">
						<text class="attr-text">库存{{ stockInfo }}</text>
					</view>
				</view>

				<!-- 操作按钮区域 -->
				<view class="action-area" v-if="showActions">
					<view class="action-btn add-cart-btn" @tap.stop="handleAddCart">
						<cl-icon name="shopping-cart" size="16" color="#ffffff"></cl-icon>
						<text class="btn-text">加购物车</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script lang="ts" setup>
import { computed, ref } from "vue";

interface Props {
	item: any;
	showTags?: boolean;
	showPriceInfo?: boolean;
	showAttrs?: boolean;
	showActions?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
	showTags: true,
	showPriceInfo: true,
	showAttrs: true,
	showActions: true,
});

const emit = defineEmits<{
	click: [item: any];
	collect: [item: any];
	"add-cart": [item: any];
}>();

// 计算属性
const isLowStock = computed(() => {
	const stock = props.item.storNum || props.item.goodsInfo?.storNum || 0;
	return Number(stock) < 10;
});

const hasNearExpiry = computed(() => {
	return !!(props.item.nearestExpiredDate || props.item.goodsInfo?.nearestExpiredDate);
});

const originalPrice = computed(() => {
	const current = props.item.price || props.item.goodsInfo?.price || 0;
	// 模拟原价，实际应从后端获取
	return current > 0 ? current * 1.2 : 0;
});

const stockInfo = computed(() => {
	const stock = props.item.storNum || props.item.goodsInfo?.storNum || 0;
	const unit = props.item.unit || props.item.goodsInfo?.unit || "";
	return `${stock}${unit}`;
});

// 方法
function formatPrice(price: any): string {
	if (!price || price === 0) return "暂无报价";
	const numPrice = Number(price);
	if (isNaN(numPrice)) return "暂无报价";
	return numPrice.toFixed(2);
}

function handleClick() {
	emit("click", props.item);
}

function handleCollect() {
	emit("collect", props.item);
}

function handleAddCart() {
	emit("add-cart", props.item);
}

function handleImageError() {
	console.error("商品图片加载失败");
}
</script>

<style lang="scss" scoped>
.enhanced-goods-item {
	padding: 8rpx;
}

.goods-card {
	background-color: #ffffff;
	border-radius: 16rpx;
	overflow: hidden;
	box-shadow: 0 2rpx 16rpx rgba(0, 0, 0, 0.08);
	transition: all 0.3s ease;

	&:active {
		transform: scale(0.98);
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.12);
	}
}

.image-container {
	position: relative;
	width: 100%;
	height: 320rpx;
	overflow: hidden;
}

.goods-image {
	width: 100%;
	height: 100%;
}

.image-overlay {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background: linear-gradient(
		to bottom,
		transparent 0%,
		transparent 70%,
		rgba(0, 0, 0, 0.1) 100%
	);
}

.collect-btn {
	position: absolute;
	top: 16rpx;
	right: 16rpx;
	width: 56rpx;
	height: 56rpx;
	border-radius: 50%;
	background-color: rgba(0, 0, 0, 0.3);
	backdrop-filter: blur(10rpx);
	display: flex;
	align-items: center;
	justify-content: center;
	transition: all 0.3s ease;

	&:active {
		transform: scale(0.9);
		background-color: rgba(0, 0, 0, 0.5);
	}
}

.tags-area {
	position: absolute;
	top: 16rpx;
	left: 16rpx;
	display: flex;
	flex-direction: column;
	gap: 8rpx;
}

.tag {
	padding: 4rpx 12rpx;
	border-radius: 12rpx;
	font-size: 20rpx;
	color: #ffffff;
	text-align: center;
	backdrop-filter: blur(10rpx);

	&.prescription-tag {
		background-color: rgba(255, 71, 87, 0.8);
	}

	&.stock-tag {
		background-color: rgba(255, 165, 0, 0.8);
	}

	&.near-expiry-tag {
		background-color: rgba(255, 193, 7, 0.8);
	}
}

.goods-info {
	padding: 20rpx 16rpx 16rpx 16rpx;
}

.goods-title {
	margin-bottom: 8rpx;
}

.title-text {
	font-size: 28rpx;
	font-weight: 500;
	color: #1a1a1a;
	line-height: 1.4;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
	overflow: hidden;
}

.goods-spec {
	margin-bottom: 12rpx;
}

.spec-text {
	font-size: 22rpx;
	color: #666;
	line-height: 1.3;
}

.price-area {
	margin-bottom: 12rpx;
}

.price-main {
	display: flex;
	align-items: baseline;
	margin-bottom: 4rpx;
}

.price-symbol {
	font-size: 24rpx;
	color: #ff4757;
	font-weight: 600;
}

.price-value {
	font-size: 32rpx;
	color: #ff4757;
	font-weight: 700;
	margin-left: 2rpx;
}

.price-info {
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.original-price {
	font-size: 20rpx;
	color: #999;
	text-decoration: line-through;
}

.sales-info {
	font-size: 20rpx;
	color: #999;
}

.goods-attrs {
	display: flex;
	flex-wrap: wrap;
	gap: 8rpx;
	margin-bottom: 16rpx;
}

.attr-item {
	padding: 4rpx 8rpx;
	background-color: #f5f5f5;
	border-radius: 8rpx;
}

.attr-text {
	font-size: 20rpx;
	color: #666;
}

.action-area {
	display: flex;
	gap: 12rpx;
}

.action-btn {
	flex: 1;
	height: 64rpx;
	border-radius: 32rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: all 0.3s ease;

	&.add-cart-btn {
		background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

		&:active {
			transform: scale(0.95);
			opacity: 0.8;
		}
	}
}

.btn-text {
	font-size: 24rpx;
	color: #ffffff;
	font-weight: 500;
	margin-left: 8rpx;
}
</style>
